import re

def check_bash_command_safety(command: str, **_):
    safe_commands = ['date', 'echo']
    if not any(c not in command for c in ';\n&|`') and ((command + '$').split())[0] in safe_commands:
        return 0
    text = command.replace('\n\r\t\v\f', '')
    dangerous_keywords = ['rm', 'mv', 'cp', 'dd', 'wget', 'curl', 'bash', 'sh', 'sudo', 'shutdown', 'pip']
    if re.search(r'\b(' + '|'.join(re.escape(keyword) for keyword in dangerous_keywords) + r')\b', text):
        return 3
    return 1

def check_python_code_safety(code: str, **_):
    text = code.replace('\n\r\t\v\f', '')
    dangerous_keywords = ['os', 'shutil', 'subprocess']
    if re.search(r'\b(' + '|'.join(re.escape(keyword) for keyword in dangerous_keywords) + r')\b', text):
        return 3
    return 1

tool_danger_timeouts = {
    'delete_tool': 5,
    'create_tool': 4,
    'delete_manual': 5,
    'create_manual': 4,
    'delete_file': 5,
    'write_file': 2,
    'append_file': 1,
    'install_packages': 4,
    'run_bash': check_bash_command_safety,
    'run_python': check_python_code_safety,
}

def get_safety_timeout(name, args):
    timeout = tool_danger_timeouts.get(name, 0)
    if callable(timeout):
        timeout = timeout(**args)
    return timeout
